package org.shock.ss.sms.commons.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {

	public static void jsonMapbean(Object obj, String str) {
		try {
			Map<String, String> map = new HashMap<String, String>();
			str = str.replaceAll("\\[", "");
			str = str.replaceAll("\\{", "");
			str = str.replaceAll("\\]", "");
			str = str.replaceAll("\\}", "");
			String[] arr = str.split(",");
			for (int i = 0; i < arr.length; i++) {
				String[] arrtmp = arr[i].split(":");
				arrtmp[1] = arrtmp[1].replaceAll("\"", "");
				Field field = obj.getClass().getDeclaredField(
						arrtmp[0].replaceAll("\"", ""));
				field.setAccessible(true);
				if (field.getType().getSimpleName().toLowerCase().equals(
						"string")) {
					field.set(obj, arrtmp[1]);
				} else if (field.getType().getSimpleName().toLowerCase()
						.equals("long")) {
					field.set(obj, Long.parseLong(arrtmp[1]));
				} else if (field.getType().getSimpleName().toLowerCase()
						.contains("int")) {
					field.set(obj, Integer.parseInt(arrtmp[1]));
				} else if (field.getType().getSimpleName().toLowerCase()
						.contains("float")) {
					field.set(obj, Float.parseFloat(arrtmp[1]));
				} else if (field.getType().getSimpleName().toLowerCase()
						.contains("double")) {
					field.set(obj, Double.parseDouble(arrtmp[1]));
				} else {
					throw new Exception("不支持的数据格式");
				}

			}
			BeanUtils.populate(obj, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	// 锟斤拷jsp页锟斤拷锟结交锟斤拷jsonlist锟斤拷锟铰凤拷装锟缴讹拷锟斤拷list锟斤拷锟皆癸拷锟斤拷crud锟斤拷锟斤拷
	public static List String2List(String json, Class cls) {
		return JSONArray.toList(JSONArray.fromObject(json), cls);
	}

	@SuppressWarnings("unchecked")
	public static List ids2List(List list, Class cls) throws Exception {
		List newList = new ArrayList();
		if (JSONArray.toArray(JSONArray.fromObject(list)).getClass() == Object[].class) {
			Object obj = cls.newInstance();
			cls.getMethod("setId", new Class[] { Integer.class }).invoke(obj,
					new Object[] { Integer.parseInt((String) list.get(0)) });
			newList.add(obj);
		} else {// 锟斤拷锟斤拷锟斤拷 页锟芥返锟截革拷式为 [{},{} ...]
			Object[] objs = (Object[]) ((Object[]) JSONArray.toArray(JSONArray
					.fromObject(list)))[0];
			for (int i = 0; i < objs.length; i++) {
				Object obj = cls.newInstance();
				cls.getMethod("setId", new Class[] { Integer.class }).invoke(
						obj, new Object[] { objs[i] });
				newList.add(obj);
			}

		}
		return newList;
	}

	@SuppressWarnings("unchecked")
	public static Object jsonStr2ConditionBean(String str, Class cls)
			throws Exception {
		Map mapCondition = new HashMap();
		Object[] objs = (Object[]) JSONArray.toArray(JSONArray.fromObject(str));
		for (Object object : objs) {
			Map map = (Map) JSONObject.toBean(JSONObject.fromObject(object),
					Map.class);
			if (map.get("comparison") != null) {
				if ("lt".equals(map.get("comparison"))) {
					mapCondition
							.put(map.get("field") + "Min", map.get("value"));
				} else if ("gt".equals(map.get("comparison"))) {
					mapCondition
							.put(map.get("field") + "Max", map.get("value"));
					;
				} else {
					mapCondition
							.put(map.get("field") + "Max", map.get("value"));
					mapCondition
							.put(map.get("field") + "Min", map.get("value"));
					mapCondition.put(map.get("field"), map.get("value"));
				}
			} else {
				mapCondition.put(map.get("field"), map.get("value"));
			}
		}
		return JSONObject.toBean(JSONObject.fromObject(mapCondition), cls);
	}
}
